package com.weapons.week0903_weapons.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.weapons.week0903_weapons.R;
import com.weapons.week0903_weapons.activity.RecommendContentActivity;
import com.weapons.week0903_weapons.adapter.RoundRecycleAdapter;
import com.weapons.week0903_weapons.bean.RecommentBean;
import com.weapons.week0903_weapons.constant.UrlString;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/7/20.
 */
public class RChildFragment extends Fragment {

    private String url;
    private Context mContext;

    private RecyclerView rl_child;
    private List<RecommentBean.ItemBean> mDatas;
    private RoundRecycleAdapter adapter;
    private LinearLayoutManager mManager;
    private Handler handler=new Handler();

    public RChildFragment() {

    }


    public static RChildFragment newInstance(String url) {
        RChildFragment fragment = new RChildFragment();
        Bundle bundle = new Bundle();
        bundle.putString("url", url);
        Log.i("pos","url=====222"+url);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();

        mDatas = new ArrayList<>();
        Bundle bundle = getArguments();
        Log.i("pos","bundle====="+bundle);
        if (bundle != null) {
            url = bundle.getString("url");

            Log.i("pos","url====="+url);
        }

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_rchild, container, false);
        return view;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        rl_child = (RecyclerView) view.findViewById(R.id.rl_child);
        super.onViewCreated(view, savedInstanceState);

        adapter = new RoundRecycleAdapter(mDatas, mContext);
        rl_child.setAdapter(adapter);
        mManager = new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL, false);
        rl_child.setLayoutManager(mManager);
        adapter.setOnItemClickListener(new RoundRecycleAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int pos, View itemView) {
                Intent intent=new Intent(mContext, RecommendContentActivity.class);
                intent.putExtra("url", String.format(UrlString.URL_RECOMMEND_CONTENT, mDatas.get(pos).getAid()));
                startActivity(intent);
            }
        });
        initData();

    }

    private void initData() {

        OkHttpClient okHttpClient = new OkHttpClient();
        Log.i("pos","url====="+url);
        Request request = new Request.Builder().url(url).build();
        Log.i("pos","request==null"+(request==null));
        Call call = okHttpClient.newCall(request);

        call.enqueue(new Callback() {


            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("pos","失败了");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.i("pos","成功");
                String result = response.body().string();
                RecommentBean bean = new Gson().fromJson(result, RecommentBean.class);
                for (int i = 0; i < bean.getItem().size(); i++) {
                    mDatas.add(bean.getItem().get(i));
                }
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        adapter.notifyDataSetChanged();
                    }
                });

                Log.i("pos","mDatas"+mDatas.size());
            }
        });
    }

}
